/**
 * Created by yangfan on 2016/11/23.
 */
define(['config/appregister',
		"i18n",
		"jquery",
		"business/myResume/services/httpServices",
		"business/services/utils",	
		],
function(app,i18n,$,httpServices,utils,constant){
    app.directive("workExpression",function(){
        return {
            templateUrl:"app/business/myResume/templates/workExpressions.html",
            scope:{
                conf:"=confData",
				cvdId:"=confCvid"
            },
            link:function(scope,element,attrs){

				scope.testShow = false ;
				scope.testBtn = function(){
					scope.testShow  = !scope.testShow ;
				}


             scope.i18n = i18n;   
             scope.curI18n = i18n.resume;
             scope.isShow  = {
             	workStart:false,
             	workEnd:false,
             	
             };
              var selectStyleSmall = {"background-color":"#fff", "width":"90px"},
        		  selectStyleBig = {"background-color":"#fff", "width":"200px"};
        	
        function getValidateArr(form,topKey){
            if(!angular.isObject(form)){
                return ;
            };
            var itemArr = [];
            for(var key in form ){
                if(key.indexOf("$")<0){
                    itemArr.push(key);
                }
            }
           return  getValidateItem(topKey,itemArr);
        }
        function getValidateItem(topKey,itemArr){
            if( toString.call(itemArr) != '[object Array]'){
                return
            }
            var flag =false;
            itemArr.forEach(function(item ){
                if(scope[topKey][item].$invalid == true){
                    scope[topKey][item].$pristine = false;
                    flag = true;
                }
            });
            return flag ;
        }
        	 
        	 scope.showComponent = function(key){
           		  scope[key].isShow = !scope[key].isShow ;
        		};
        	
        	 scope.changeShow = function(key){
            	if(scope.isShow.hasOwnProperty(key)){
                scope.isShow[key] = !scope.isShow[key];
           	 }
        };
                //工作经验
         
           scope.company = "";
           scope.department = "";
           scope.description = "";
       		  //工作时间
      	   scope.workExpression = {
            startValue:"",
            endValue:"",
            isShow:false
        	};
				//单选
            	//职能
        	scope.workExpreJobConf = {
           		top:"",
            	detail:[],
            	showValue:"",
            	isShow:false,
				maxSelect:1
        	};
        	//行业
        	scope.workExpreIndustryConf = {
            	detail:[],
            	showValue:"",
            	isShow:false,
				maxSelect:1
        	};
        	
           scope.workExpressSelect = {
        	salary:{
        		model:"",
                value:scope.curI18n.placeholderSelect,
                key:"srName",
                style:selectStyleBig
        	},
        	companyNature:{
        		model:"",
                value:scope.curI18n.placeholderSelect,
                key:"dictName",
                style:selectStyleBig
        	},
        	companyScale:{
        		model:"",
                value:scope.curI18n.placeholderSelect,
                key:"dictName",
                style:selectStyleBig
        	},
        	jobNature:{
        		model:"",
                value:scope.curI18n.placeholderSelect,
                key:"dictName",
                style:selectStyleBig
        	},
        	errMessage:{
        		salary:false,
        		companyNature:false,
        		jobNature:false
        	}       	       	      	       	       	
        };
				scope.curentCalendarLeave = function(){
					scope.isShow.workStart = false ;
					scope.isShow.workEnd = false ;
				}
        	
        	//公司规模
        	function getCompanyScale(){
        		var req = {
        			type:"GET",
        			uri:"getCompanyScale"
        		};
        		httpServices.getCvdatabases(req).success(function(response){ 
            			scope.workExpressSelect.companyScale.model = response.data;
            		}).error(function(){}) 
        		
        	}
        	//公司性质
        	function getCompanyNature(){
        		var req = {
        			type:"GET",
        			uri:"getCompanyNature"
        		};
        		httpServices.getCvdatabases(req).success(function(response){ 
            			scope.workExpressSelect.companyNature.model = response.data;
            		}).error(function(){}) 
        		
        	}


        	//工作性质
        	function getJobNature(){
            		var req = {
            			type:"GET",
            			uri:"getJobNature"
            		};
            		httpServices.jobServices(req).success(function(response){
            			scope.workExpressSelect.jobNature.model = response.data;
            		}).error(function(){})           		
            	}
            	//
        	//薪资水平
        	function getSalaryCount(){
            		var req = {
            			type:"GET",
            			uri:"getSalaryCount"
            		};
            		httpServices.getCvdatabases(req).success(function(response){ 
            			scope.workExpressSelect.salary.model = response.data;
            		}).error(function(){})           		
            	}
        	function init(){
        		getCompanyScale();
        		getCompanyNature();
        		getJobNature();
        		getSalaryCount()       		
        	}
				function _isValidateSelect(){
					var flag = false ;
					scope.workExpressSelect.errMessage.companyNature= false ;
					scope.workExpressSelect.errMessage.jobNature = false;
					if(scope.workExpressSelect.companyNature.value== scope.curI18n.placeholderSelect){
						scope.workExpressSelect.errMessage.companyNature= true;
						flag = true ;
					}
					if(scope.workExpressSelect.jobNature.value== scope.curI18n.placeholderSelect){
						scope.workExpressSelect.errMessage.jobNature = true;
						flag = true
					}
					return flag

				}
        	scope.workExpressSubmit = function(){
        		if(scope.workExpressionForm.$invalid || _isValidateSelect()){
					var  topKey = "workExpressionForm";
                	var flag = getValidateArr(scope.workExpressionForm,topKey);
                ////
                if(scope.workExpressSelect.companyNature.value== scope.curI18n.placeholderSelect){
                    scope.workExpressSelect.errMessage.companyNature= true;
                }

                //工作性质
                if(scope.workExpressSelect.jobNature.value== scope.curI18n.placeholderSelect){	
                    scope.workExpressSelect.errMessage.jobNature = true;
                } 

            }else{
            	var guildObj = {
            		dictId:[]
            	};
            	var fcObj = {
            		dictId:[]
            	};
            	var req = {
            		type:"POST",
            		uri:"saveWorkExp",
            		data:{
						startTime:utils.YMD2YMD(scope.workExpression.startValue) ,
						endTime:utils.YMD2YMD(scope.workExpression.endValue),
						//行业
						guildId:scope.workExpreIndustryConf.detail[0].dictId,
						companyName:scope.company,
						//公司规模
						scaleId:scope.workExpressSelect.companyScale.value ==""?"":scope.workExpressSelect.companyScale.value.dictId,
						// 职能
						//fdcId:utils.getMulti(scope.workExpreJobConf.detail,fcObj ),
						fdcId:scope.workExpreJobConf.detail[0].fdcId,
						//公司性质
						natureId:scope.workExpressSelect.companyNature.value == ""?"":scope.workExpressSelect.companyNature.value.dictId,
						//部门
						department:scope.department ,
						//工作性质
						jobNatureId:scope.workExpressSelect.jobNature.value == ""?"":scope.workExpressSelect.jobNature.value.dictId,
						salaryId:scope.workExpressSelect.salary.value == ""?"":scope.workExpressSelect.salary.value.srId,
						introduction:scope.description
            		}
            	};
					if(scope.isUpdate == true){
						req.uri = "updateWorkExp";
						req.data.cvdId = scope.conf.cvdId;
						req.data.weId = scope.conf.weId;
					}
            	 httpServices.getCvdatabases(req).success(function(response){
                    if(response.msg == "success"){
						if(scope.isUpdate == true){
							var obj = {
								key1:"showWorkexperience",
								key2:"showForm",
								type:0
							}
						}else{
							var obj = {
								key1:"isInitAddNew",
								key2:"workExpression",
								type:0
							}
						}
						scope.$emit("reGetResumeInfo",obj);
                    }
                }).error(function(response){
                })
            }
        	};
		    scope.cancelWorkExpressSubmit = function(){
				if(scope.isUpdate == true){
					var obj = {
						key1:"showWorkexperience",
						key2:"showForm",
						type:1
					}
				}else{
					var obj = {
						key1:"isInitAddNew",
						key2:"workExpression",
						type:1
					}
				}
				scope.$emit("reGetResumeInfo",obj);
			};

				// 表示是否修改
				scope.isUpdate = false ;
				if(typeof scope.conf!="undefined"){
					fillData()
				}else{
					init();
				}
				function fillData(){
					scope.workExpression.startValue = utils.getYMD(scope.conf.startValue);
					scope.workExpression.endValue = utils.getYMD(scope.conf.endValue);
					//行业（单选）
					scope.workExpreIndustryConf.detail[0] = scope.conf.workExpreIndustryConf;
					scope.workExpreIndustryConf.showValue = scope.conf.workExpreIndustryConf.dictName;

					scope.company = scope.conf.companyName;
					scope.workExpressSelect.companyScale.value = scope.conf.companyScale;

					scope.workExpreJobConf.detail[0] = scope.conf.funcd;
					scope.workExpreJobConf.showValue = scope.conf.funcd.fdcName;

					scope.workExpressSelect.companyNature.value = scope.conf.companyScale;

					scope.department = scope.conf.department;
					scope.workExpressSelect.jobNature.value =  scope.conf.jobNature;
					scope.workExpressSelect.salary.value = scope.conf.salary;
					scope.description = scope.conf.introduction;
					scope.isUpdate = true ;
					init();

				}


                
             
            }
        }
    })
});